package com.ruoyi.plugs.blog.common;


import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

public class DataUtil {

    private static DataUtil dataUtil;

    private DataUtil() {

    }

    public static synchronized DataUtil getInstance() {
        if (dataUtil == null) {
            dataUtil = new DataUtil();
        }
        return dataUtil;
    }

    public Map<String, String> paramToMap(Map<String, String[]> paramsMap) {
        return paramToMap(paramsMap, null);
    }

    public Map<String, String> paramToMap(Map<String, String[]> paramsMap, Predicate<String> filter) {
        if (paramsMap != null) {
            Map<String, String> map = new HashMap<>();
            paramsMap.keySet().forEach(key -> {
                if (filter != null) {
                    if (filter.test(key)) {
                        String[] o = paramsMap.get(key);
                        if (o != null && o.length == 1) {
                            map.put(key, o[0]);
                        } else {
                            map.put(key, StringUtils.toString(o));
                        }
                    }
                } else {
                    String[] o = paramsMap.get(key);
                    if (o != null && o.length == 1) {
                        map.put(key, o[0]);
                    } else {
                        map.put(key, StringUtils.toString(o));
                    }
                }
            });
            return map;

        }
        return null;
    }
}
